<template>
  <view class="search-goods" v-else-if="onSearchView">
    <view class="search-goods-header">
      <view class="search-view">
        <view class="bar-code-input-area">
          <input type="text" placeholder-class="bar-code-placeholder" class="" placeholder="商品名称/助记码/商品条码"
            v-model="searchValue" @confirm="add2cart" />
          <!-- <image class="bar-code-icon" src="/static/icons/bar-code.png" mode="aspectFit"></image> -->
        </view>
        <button class="right-button text-button" @click="cancel">取消</button>
      </view>
    </view>
    <view class="search-goods-content">
      <view class="search-goods-content-item tips" v-if="matchedGoods.length===0">
        {{searchValue?'暂无商品':"请在上方输入商品名称/商品条码/助记码进行搜索"}}
      </view>
      <view class="search-goods-content-item" v-else>
        <view class="goods-item"
          :class="{active:item.goods_barcode===curGoods,disabled:item.sale_state=='售罄'||item.goods_stock<=0||(cartGoodsAmounts&&cartGoodsAmounts[item.goods_no]&&item.goods_stock<=cartGoodsAmounts[item.goods_no])}"
          v-for="item in matchedGoods" @click="confirm(item)">
          <view class="badge inner" v-if="item.sale_state==='售罄'">
            售罄
          </view>
          <view class="badge inner" v-else-if="item.goods_stock<=0||(cartGoodsAmounts&&cartGoodsAmounts[item.goods_no]&&item.goods_stock<=cartGoodsAmounts[item.goods_no])">
            库存不足
          </view>
          <view class="top">
            <text class="left"> {{item.goods_name}}</text>
            <text>￥{{item.sale_price}}</text>
          </view>
          <view class="bottom">
            <text>{{item.goods_barcode||'-'}}</text>
            <text>{{item.goods_unit_name||'-'}}</text>
          </view>
        </view>
      </view>
      <view class="search-goods-content-item-right">
        <pinyin-keyboard @change="changeNumber" @backspace="backspace"></pinyin-keyboard>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "search-goods",
    data() {
      return {
        searchValue: "",
        curGoods: ''
      };
    },
    props: {
      cartGoodsAmounts: {
        type: Object
      },
    },
    computed: {

      goodsList() {
        return this.$store?.state?.$goodsList || []
      },
      matchedGoods() {
        return this.goodsList.filter(item => this.matchText(item.goods_name) || this.matchText(item.goods_barcode) ||
          this.matchText(item.mnemonic_code))
      }
    },
    methods: {
      add2cart(e) {
        console.log(e);
        const barCode = e?.detail.value || this.searchValue
        this.$emit('add2cart', barCode)
      },
      changeNumber(num) {
        let result = `${this.searchValue}${num}`
        if (result) {

        }
        this.searchValue = result
      },
      backspace() {
        if (this.searchValue) {
          this.searchValue = this.searchValue.slice(0, this.searchValue.length - 1)
        }
      },
      confirm(e) {
        this.curGoods = e.goods_barcode
        this.$emit('confirm', e.goods_barcode)
      },
      matchText(str) {
        return this.searchValue && str && str.indexOf(this.searchValue) > -1
      },
      cancel() {
        this.$emit('cancel')
      }
    },
  }
</script>

<style lang="scss" scoped>
  .search-goods {

    flex: 1;
    height: 100%;

    background-color: #f2f2f2;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;

    &-header {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .search-view {
        // max-height: 180rpx;
        display: flex;
        position: relative;
        padding: 2rem;
        width: calc(100% - 4rem);

        .bar-code-input-area {
          background-color: #fff;
          // margin: 0 1rem;
          border-radius: 1rem;
          height: 6rem;
          padding: 1.6rem 2.4rem;
          flex: 1;
          position: relative;
          display: flex;
          align-items: center;

          input {
            flex: 1;
            font-size: 2.4rem
          }

          .bar-code-icon {
            width: 3.2rem;
            height: 3.2rem;
          }
        }

        .bar-code-placeholder {
          color: #D5D5D5;
          font-size: 2rem;
        }

        .right-button {
          font-size: 3rem;
          width: 8.8rem;
          height: 6rem;
          text-align: center;
          line-height: 6rem;
          border-radius: 1rem;
          background-color: #fff;
          margin-left: 2rem;
          font-weight: 700;
          padding: 0;
          display: flex;
          align-items: center;
          justify-content: center;

          &.text-button {
            font-size: 2.3rem;
            font-weight: 500;
            overflow: hidden;
            background-color: #E5E5E5;
            border: none;

            &::after {
              content: none;
            }
          }
        }
      }

    }

    &-content {
      display: flex;
      flex: 1;
      overflow: hidden;
      background-color: #fff;
      border-radius: 1rem;
      margin: 0 2rem 2rem;
      padding: 2rem;
      overflow: hidden;
      justify-content: space-between;

      &-item {
        flex: 1;
        overflow: hidden;
        overflow-y: scroll;
        min-width: 50rem;

        &.tips {
          display: flex;
          align-items: center;
          justify-content: center;
          color: #ccc;
          font-size: 2.4rem;
        }

        &:first-child {
          margin-right: 2rem;
        }

        .goods-item {
          width: 100%;
          border-radius: 1rem;
          background-color: #F5F5F5;
          border: 1px solid #F5F5F5;
          padding: 1.3rem 2rem;
          margin-bottom: 1rem;
          max-width: 60rem;
          overflow-x: hidden;
          display: flex;
          flex-direction: column;
          position: relative;

          &.disabled {
            opacity: 0.6;
          }

          .badge {
            position: absolute;
            background-color: transparent;
            left: 50%;
            top: 10%;
            font-size: 3rem;
            color: #ccc;
            transform: rotate(-20deg) translateX(-50%);
          }

          &.active {
            background: $retail-color-primary-light;
            border-color: $retail-color-primary;
          }

          .top {
            padding-right: 2rem;
            font-size: 2.4rem;
            font-weight: 700;
            line-height: 4.2rem;
            display: flex;
            justify-content: space-between;
            width: 100%;
            overflow: hidden;

            .left {
              overflow: hidden;
              flex: 1;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }

          .bottom {
            font-size: 2rem;
            display: flex;
            justify-content: space-between;
          }
        }
      }
    }
  }
</style>
